package com.neusoft.bizcore.schedule.controller;

import org.quartz.JobKey;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.schedule.converter.JobConverter;
import com.neusoft.bizcore.schedule.converter.TriggerConverter;
import com.neusoft.bizcore.schedule.dto.JobDetailDTO;
import com.neusoft.bizcore.schedule.dto.TriggerDTO;
import com.neusoft.bizcore.schedule.service.JobMgrService;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.support.Searchable;

/**
 * description
 *
 * @author sunchf
 * @date 2018年12月26日 下午2:18:01
 */
@RestController
@RequestMapping("/schedule")
public class JobMgrController {
    @Autowired
    private JobMgrService jobMgrService;
    @Autowired
    private JobConverter jobConverter;
    @Autowired
    private TriggerConverter triggerConverter;

    @GetMapping("/jobs")
    public PageResultDTO<JobDetailDTO> getJobs(final Searchable searchable, final Pageable pageable) {
        final Page<JobKey> pageModels = this.jobMgrService.getJobs(searchable, pageable);
        final Page<JobDetailDTO> page = this.jobConverter.toPageDTO(pageModels);
        return PageResultDTO.success(page);
    }

    @GetMapping("/triggers")
    public PageResultDTO<TriggerDTO> getTriggers(final Searchable searchable, final Pageable pageable) {
        final Page<Trigger> pageModels = this.jobMgrService.getTriggers(searchable, pageable);
        final Page<TriggerDTO> page = this.triggerConverter.toPageDTO(pageModels);
        return PageResultDTO.success(page);
    }

    @PutMapping("/{jobName}/pauseJob")
    public ResultDTO<Boolean> pauseJob(@PathVariable String jobName) {
        return ResultDTO.success(this.jobMgrService.pauseJob(jobName));
    }

    @PutMapping("/{group}/{name}/pauseJob")
    public ResultDTO<Boolean> pauseJobWithGroup(@PathVariable String group, @PathVariable String name) {
        return ResultDTO.success(this.jobMgrService.pauseJob(name, group));
    }

    @PutMapping("/{jobName}/resumeJob")
    public ResultDTO<Boolean> resumeJob(@PathVariable String jobName) {
        return ResultDTO.success(this.jobMgrService.resumeJob(jobName));
    }

    @PutMapping("/{group}/{name}/resumeJob")
    public ResultDTO<Boolean> resumeJobWithGroup(@PathVariable String group, @PathVariable String name) {
        return ResultDTO.success(this.jobMgrService.resumeJob(name, group));
    }

    @DeleteMapping("/{jobName}/deleteJob")
    public ResultDTO<Boolean> deleteJob(@PathVariable String jobName) {
        return ResultDTO.success(this.jobMgrService.deleteJob(jobName));
    }

    @DeleteMapping("/{group}/{name}/deleteJob")
    public ResultDTO<Boolean> deleteJobWithGroup(@PathVariable String group, @PathVariable String name) {
        return ResultDTO.success(this.jobMgrService.deleteJob(name, group));
    }

    @PutMapping("/{triggerKey}/pauseTrigger")
    public ResultDTO<Boolean> pauseTrigger(@PathVariable String triggerKey) {
        return ResultDTO.success(this.jobMgrService.pauseTrigger(triggerKey));
    }

    @PutMapping("/{triggerKey}/resumeTrigger")
    public ResultDTO<Boolean> resumeTrigger(@PathVariable String triggerKey) {
        return ResultDTO.success(this.jobMgrService.resumeTrigger(triggerKey));
    }

    @PutMapping("/{triggerKey}/deleteTrigger")
    public ResultDTO<Boolean> deleteTrigger(@PathVariable String triggerKey) {
        return ResultDTO.success(this.jobMgrService.deleteTrigger(triggerKey));
    }
}
